package org.springframework.mybatis.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * <p>解析和存储配置信息</p>
 * @author Bosen
 * @date 2021/9/27 21:49
 */
public class Configuration {
    /**
     * <p>连接数据库必要的信息</p>
     */
    private String driver, url, username, password;

    /**
     * <p>映射器所在的包</p>
     */
    private String mapperPackage;

    private Properties config = new Properties();

    /**
     * <p>通过用户传入的路径读取配置文件</p>
     */
    public Configuration(String location) {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(
                location.replace("classpath:", "")
        );
        try {
            this.config.load(is);
            // 驱动类
            this.driver = this.config.getProperty("mini.jdbc.driver");
            // 服务地址
            this.url = this.config.getProperty("mini.jdbc.url");
            // 用户名
            this.username = this.config.getProperty("mini.jdbc.username");
            // 密码
            this.password = this.config.getProperty("mini.jdbc.password");
            // 映射器所在的包
            this.mapperPackage = this.config.getProperty("mini.mybatis.mapper.package");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Properties getConfig() {
        return config;
    }

    public void setConfig(Properties config) {
        this.config = config;
    }

    public String getMapperPackage() {
        return mapperPackage;
    }

    public void setMapperPackage(String mapperPackage) {
        this.mapperPackage = mapperPackage;
    }
}
